package com.oa.aop;

import java.lang.reflect.Method;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.oa.bean.Log;
import com.oa.service.LogService;


@Aspect
@Component
public class SystemLogAspect {

	@Autowired
	LogService logService;
	
	@Pointcut("@annotation(com.oa.aop.LogAnnotation)")  
	      public  void controllerAspect() {  
	 }  
	
	/**
	 * 后置通知
	 */
	@After("controllerAspect()") 
	public void afterPointCut(JoinPoint joinPoint){
		  HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		  HttpSession session = request.getSession();
		  Integer uid = (Integer) session.getAttribute("uid");
		
		  try {
			  //获取文件名
			  String targetName =joinPoint.getTarget().getClass().getName();
			  //获取方法名
			  String methodName = joinPoint.getSignature().getName();
			  Object[]arguments = joinPoint.getArgs();
			  Class targetClass = Class.forName(targetName);
			  Method[]methods = targetClass.getMethods();
			  String description = "";
			  for(Method method :methods){
				  if(method.getName().equals(methodName)){
					  Class[]clazzs = method.getParameterTypes();
					  if(clazzs.length == arguments.length){
						  description = method.getAnnotation(LogAnnotation.class).description();
						  break;
					  }
				  }
			  }
			  System.out.println("后置通知开始");
			  System.out.println("所做的事情"+description);
			  Log log = new Log();
			  log.setUserid(uid);
			  log.setOperation(description);
			  log.setType("正常");
			  String result = logService.addLog(log);                                                                                                                                                                                                                                                                                                                                                                                                                                                     
			  System.out.println(result);
			  
			  
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  
	}
	
	/**
	 * 拦截发生的异常
	 * @param joinPoint
	 * @param e
	 */
	
	 @AfterThrowing(pointcut = "controllerAspect()", throwing="e")  
	public  void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
		 HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		 HttpSession session = request.getSession();
		 String ip = request.getRemoteAddr();
		 System.out.println(ip);
		 Integer uid = (Integer) session.getAttribute("uid");
		 
		 try {
			 String targetName = joinPoint.getTarget().getClass().getName();
			 String methodName = joinPoint.getSignature().getName();
			 Object[]arguments = joinPoint.getArgs();
			Class targetClass = Class.forName(targetName);
			Method[]methods = targetClass.getMethods();
			String description = "";
			for(Method method : methods){
				Class[]clazzs = method.getParameterTypes();
				if(clazzs.length==arguments.length){
					description = method.getAnnotation(LogAnnotation.class).description();
					break;
				}
			}
			System.out.println("异常通知开始了");
			Log log = new Log();
			log.setUserid(uid);
			log.setType("异常");
			log.setOperation(description+"发生了异常");
			String result = logService.addLog(log);
			System.out.println("添加操作日志的结果"+result);
			
		} catch (ClassNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		 
	}
	
}
















